﻿using System;
using System.Net.Mime;

namespace Thi_nghiem_nen_ket_co_thong_tin
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCpApLuc();
            }
        }

        protected void Calculate()
        {
            var objTn = (ObjThiNghiem)Session[ObjThiNghiem.SESSION_NAME];
            if (objTn == null)
            {
                objTn=new ObjThiNghiem();
                objTn.P[0] = 0;
                Session[ObjThiNghiem.SESSION_NAME] = objTn;
            }

            double data_chieu_cao_bef;
            double data_duong_kinh_bef;
            double data_do_am_bef;
            double data_ty_trong_bef;
            double data_dung_trong_uot_bef;
            double data_dien_tich_bef;
            double data_the_tich_bef;
            double data_khoi_luong_bef;
            double data_dung_trong_kho_bef;
            double data_do_bao_hoa_bef;
            double data_he_so_rong_bef;

            double data_chieu_cao_aft;
            double data_duong_kinh_aft;
            double data_do_am_aft;
            double data_ty_trong_aft;
            double data_dung_trong_uot_aft;
            double data_dien_tich_aft;
            double data_the_tich_aft;
            double data_khoi_luong_aft;
            double data_dung_trong_kho_aft;
            double data_do_bao_hoa_aft;
            double data_he_so_rong_aft;

//            string cap_ap_luc = string.Empty;
//            string str_P = string.Empty;
//            string str_AH = string.Empty;
//            string str_e = string.Empty;
//            string str_a = string.Empty;
//            string str_T50 = string.Empty;
//            string str_S50 = string.Empty;
//            string str_Cv = string.Empty;
//            string str_Kv = string.Empty;
//            string str_mv = string.Empty;

//            if (cap_ap_luc != "")
//            {
//                int i = Convert.ToInt16(cap_ap_luc.Substring(4, 1));
//                objTn.P[i] = Convert.ToDouble(str_P);
//                objTn.AH[i] = Convert.ToDouble(str_AH);
//                objTn.hsr[i] = Convert.ToDouble(str_e);
//                objTn.a[i] = Convert.ToDouble(str_a);
//                objTn.T50[i] = Convert.ToDouble(str_T50);
//                objTn.S50[i] = Convert.ToDouble(str_S50);
//                objTn.Cv[i] = Convert.ToDouble(str_Cv);
//                objTn.Kv[i] = Convert.ToDouble(str_Kv);
//                objTn.mv[i] = Convert.ToDouble(str_mv);
//            }
            //cong thuc tinh toan mau truoc thi nghiem
            if (txt_chieu_cao_mau_bef.Text != "" && txt_duong_kinh_mau_bef.Text != "" &&
                txt_do_am_bef.Text != "" && txt_ty_trong_bef.Text != "" && txt_dung_trong_uot_bef.Text != "")
            {
                data_chieu_cao_bef = Convert.ToSingle(txt_chieu_cao_mau_bef.Text);
                data_duong_kinh_bef = Convert.ToSingle(txt_duong_kinh_mau_bef.Text);
                data_do_am_bef = Convert.ToSingle(txt_do_am_bef.Text);
                data_ty_trong_bef = Convert.ToSingle(txt_ty_trong_bef.Text);
                data_dung_trong_uot_bef = Convert.ToSingle(txt_dung_trong_uot_bef.Text);

                data_dien_tich_bef = 3.14 * data_duong_kinh_bef * data_duong_kinh_bef / 4;
                data_the_tich_bef = data_dien_tich_bef * data_chieu_cao_bef;
                data_khoi_luong_bef = data_the_tich_bef * data_dung_trong_uot_bef;
                data_dung_trong_kho_bef = data_dung_trong_uot_bef / (1 + 0.01 * data_do_am_bef);
                data_he_so_rong_bef = data_ty_trong_bef / data_dung_trong_kho_bef - 1;
                data_do_bao_hoa_bef = ((0.01 * data_do_am_bef * data_ty_trong_bef) / data_he_so_rong_bef) * 100;

                txt_dien_tich_bef.Text = Convert.ToString(Math.Round(data_dien_tich_bef, 2));
                txt_the_tich_mau_bef.Text = Convert.ToString(Math.Round(data_the_tich_bef, 2));
                txt_khoi_luon_mau_bef.Text = Convert.ToString(Math.Round(data_khoi_luong_bef, 2));
                txt_dung_trong_kho_bef.Text = Convert.ToString(Math.Round(data_dung_trong_kho_bef, 2));
                txt_do_bao_hoa_bef.Text = Convert.ToString(Math.Round(data_do_bao_hoa_bef, 2));
                txt_he_so_rong_bef.Text = Convert.ToString(Math.Round(data_he_so_rong_bef, 3));
            }
            //cong thuc tinh toan mau sau thi nghiem
            if (txt_chieu_cao_mau_aft.Text != "" && txt_duong_kinh_mau_aft.Text != "" &&
                txt_do_am_aft.Text != "" && txt_ty_trong_aft.Text != "" && txt_dung_trong_uot_aft.Text != "")
            {
                data_chieu_cao_aft = Convert.ToSingle(txt_chieu_cao_mau_aft.Text);
                data_duong_kinh_aft = Convert.ToSingle(txt_duong_kinh_mau_aft.Text);
                data_do_am_aft = Convert.ToSingle(txt_do_am_aft.Text);
                data_ty_trong_aft = Convert.ToSingle(txt_ty_trong_aft.Text);
                data_dung_trong_uot_aft = Convert.ToSingle(txt_dung_trong_uot_aft.Text);

                data_dien_tich_aft = 3.14 * data_duong_kinh_aft * data_duong_kinh_aft / 4;
                data_the_tich_aft = data_dien_tich_aft * data_chieu_cao_aft;
                data_khoi_luong_aft = data_the_tich_aft * data_dung_trong_uot_aft;
                data_dung_trong_kho_aft = data_dung_trong_uot_aft / (1 + 0.01 * data_do_am_aft);
                data_he_so_rong_aft = data_ty_trong_aft / data_dung_trong_kho_aft - 1;
                data_do_bao_hoa_aft = ((0.01 * data_do_am_aft * data_ty_trong_aft) / data_he_so_rong_aft) * 100;

                txt_dien_tich_aft.Text = Convert.ToString(Math.Round(data_dien_tich_aft, 2));
                txt_the_tich_mau_aft.Text = Convert.ToString(Math.Round(data_the_tich_aft, 2));
                txt_khoi_luong_mau_aft.Text = Convert.ToString(Math.Round(data_khoi_luong_aft, 2));
                txt_dung_trong_kho_aft.Text = Convert.ToString(Math.Round(data_dung_trong_kho_aft, 2));
                txt_do_bao_hoa_aft.Text = Convert.ToString(Math.Round(data_do_bao_hoa_aft, 2));
                txt_he_so_rong_aft.Text = Convert.ToString(Math.Round(data_he_so_rong_aft, 3));
            }
            if (txt_ty_trong_bef.Text != "")
                txt_ty_trong_aft.Text = txt_ty_trong_bef.Text;
            if (txt_duong_kinh_mau_bef.Text != "")
                txt_duong_kinh_mau_aft.Text = txt_duong_kinh_mau_bef.Text;
        }

        protected void btCalCulate_Click(object sender, EventArgs e)
        {
            Calculate();
        }

        protected void btReset_Click(object sender, EventArgs e)
        {
            txt_do_am_bef.Text = txt_ty_trong_bef.Text = txt_dung_trong_uot_bef.Text =
                txt_chieu_cao_mau_bef.Text = txt_duong_kinh_mau_bef.Text = txt_dien_tich_bef.Text =
                    txt_the_tich_mau_bef.Text = txt_khoi_luon_mau_bef.Text = txt_dung_trong_kho_bef.Text =
                        txt_do_bao_hoa_bef.Text = txt_he_so_rong_bef.Text = "";

            txt_do_am_aft.Text = txt_ty_trong_aft.Text = txt_dung_trong_uot_aft.Text =
                txt_chieu_cao_mau_aft.Text = txt_duong_kinh_mau_aft.Text = txt_dien_tich_aft.Text =
                    txt_the_tich_mau_aft.Text = txt_khoi_luong_mau_aft.Text = txt_dung_trong_kho_aft.Text =
                        txt_do_bao_hoa_aft.Text = txt_he_so_rong_aft.Text = "";
            Session[ObjThiNghiem.SESSION_NAME] = null;
        }

        protected void txt_so_cap_ap_luc_ValueChanged(object sender, EventArgs e)
        {
            BindCpApLuc();
        }

        protected void BindCpApLuc()
        {
            lblCapApLucErr.Text = "";
            if (Convert.ToInt16(txt_so_cap_ap_luc.Text) > 9)
            {
                lblCapApLucErr.Text = "*Số cấp quá lớn so với quy định !!";
                return;
            }
            cm_cap_ap_luc.Items.Clear();
            for (int i = 1; i <= Convert.ToInt16(txt_so_cap_ap_luc.Text); i++)
            {
                string[] aaa = { "Cấp " + i};
                cm_cap_ap_luc.Items.AddRange(aaa);
            }
            string[] bbb = { "Áp lực nén", "aaa" };
            cm_cap_ap_luc.Items.AddRange(bbb);
        }

        protected void cm_cap_ap_luc_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblCapApLucErr.Text = "";
            if (txt_he_so_rong_bef.Text == "")
            {
                lblCapApLucErr.Text = "*Chưa nhập thông số đầu vào... Xem lại dữ liệu !!";
                    return;
            }
            var objTn = (ObjThiNghiem)Session[ObjThiNghiem.SESSION_NAME];
            if(objTn==null) return;
            
            if (cm_cap_ap_luc.SelectedItem.ToString() == "Áp lực nén")
            {
                //int i = Convert.ToInt16(txt_so_cap_ap_luc.Text);
                //Bieu_do_log_cuoi_thi_nghiem frm1 = new Bieu_do_log_cuoi_thi_nghiem(i, P, AH, hsr);
                //frm1.Show();
                Bieu_do_log_cuoi_thi_nghiem1.NumOfData = string.IsNullOrEmpty(txt_so_cap_ap_luc.Text)
               ? 0
               : Convert.ToInt32(txt_so_cap_ap_luc.Number);
                Bieu_do_log_cuoi_thi_nghiem1.LoadData(objTn.P,objTn.AH,objTn.hsr);
                pupBdLogTn.ShowOnPageLoad = true;
            }
            else if (cm_cap_ap_luc.SelectedItem.ToString() == "aaa")
            {
                Cv_P_and_Kv_e1.NumOfData = string.IsNullOrEmpty(txt_so_cap_ap_luc.Text)
               ? 0
               : Convert.ToInt32(txt_so_cap_ap_luc.Number);
                Cv_P_and_Kv_e1.LoadData();
                pupCvpEv.ShowOnPageLoad = true;
            }
            else
            {
                objTn.hsr[0] = Convert.ToDouble(txt_he_so_rong_bef.Text);
                int i = Convert.ToInt16(cm_cap_ap_luc.SelectedItem.ToString().Substring(4, 1));
                Bieu_do_thi_nghiem1.SetHValue(cm_cap_ap_luc.SelectedItem.ToString(), "", objTn.P[i - 1].ToString(),
                 objTn.hsr[0].ToString(), objTn.hsr[i - 1].ToString());
                pupBieuDoThiNghiem.ShowOnPageLoad = true;
            }
        }

        protected void btShowResult_Click(object sender, EventArgs e)
        {
            Ket_qua_thi_nghiem1.NumOfData = string.IsNullOrEmpty(txt_so_cap_ap_luc.Text)
                ? 0
                : Convert.ToInt32(txt_so_cap_ap_luc.Number);
            Ket_qua_thi_nghiem1.LoadData();
            pupKqThiNghiem.ShowOnPageLoad = true;
        }
    }
}